package com.thread.test;

public class TwoDimensionalArraySum {

    private static final int RUNS = 100;
    private static final int DIMENSION_1 = 1024*1024;
    private static final int DIMENSION_2 = 6;
    private static long[][] longs;

    public static void main(String[] args) {

        longs = new long[DIMENSION_1][];
        for(int i=0; i<DIMENSION_1; i++){
            longs[i] = new long[DIMENSION_2];
            for(int j=0; j<DIMENSION_2; j++){
                longs[i][j] = 1L;
            }
        }
        System.out.println("Array init done");

        long sum = 0L;
        long start = System.currentTimeMillis();
        for(int n=0; n<RUNS; n++){
            for(int i=0; i<DIMENSION_1; i++){
                for(int j=0; j<DIMENSION_2; j++){
                    sum += longs[i][j];
                }
            }
        }
        long end = System.currentTimeMillis();
        System.out.println(sum + " spend time1:" + (end - start));

        sum = 0L;
        start = System.currentTimeMillis();
        for(int n=0; n<RUNS; n++){
            for(int j=0; j<DIMENSION_2; j++){
                for(int i=0; i<DIMENSION_1; i++){
                    sum += longs[i][j];
                }
            }
        }
        end = System.currentTimeMillis();
        System.out.println(sum + " spend time2:" + (end - start));

    }

}
